﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using HRM.BusinessLogic;

namespace HRM
{
    public partial class frmNgachBac : FormBase
    {
        DataTable dtNgach, dtBac;
        int iNgachId;
        //

        public frmNgachBac()
        {
            InitializeComponent();
        }

        public override void Show_Form()
        {
            this.Show();
        }

        bool isFirstLoad = true;
        protected override void Init_Control()
        {
            try
            {
                Init_Grid();
                //
                LoadNgach();
                //isFirstLoad = false;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        //Khởi tạo danh sách cho grid
        DataGridHandle m_GridHandle_Ngach, m_GridHandle_Bac;
        protected void Init_Grid()
        {
            List<DataGridViewColumn> arrColumn = new List<DataGridViewColumn>();

            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("STT", "RN", 30, "", true));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("GradeGUID", "GradeGUID", 30, "", true));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("ID", "Grade_ID", 30,"",true));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Đơn vị", "Grade_BranchCode", 40,"",true));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Tên chức danh", "Grade_Name", 180));
            //
            m_GridHandle_Ngach = FormGlobals.Grid_Init(dgvNgach, arrColumn, "", 1);
            m_GridHandle_Ngach.Row_Validated += new DataGridHandle.Row_ValidatedEventHandler(m_GridHandle_Ngach_Row_Validated);
            m_GridHandle_Ngach.Row_Deleting += new DataGridHandle.Row_DeletingEventHandler(m_GridHandle_Ngach_Row_Deleting);
            m_GridHandle_Ngach.Row_Selected(new MethodInvoker(() => {

                DataRow row = m_GridHandle_Ngach.GetRowData();

                if (row != null)
                {
                    if (String.IsNullOrEmpty(Globals.Object_GetString(row["GradeGUID"], "")))
                        row["GradeGUID"] = Guid.NewGuid().ToString();

                    //Load danh sách bậc tương ứng
                    int gradeId = m_GridHandle_Ngach.GetRowValue<int>("Grade_ID");
                    string guid = m_GridHandle_Ngach.GetRowValue<string>("GradeGUID");
                    if (isFirstLoad)
                    {
                        dtBac = Grade_Controller.Instance().Grade_ListByBranch(Program.strBranchCode);
                        dtBac.Columns.Add("ParentGUID");
                        dgvBac.DataSource = dtBac;
                        dtBac.Columns["GradeDetail_BranchCode"].DefaultValue = Program.strBranchCode;
                        dtBac.DefaultView.RowFilter = "(GradeDetail_GradeID = " + gradeId.ToString() + " AND GradeDetail_GradeID > 0) OR (ParentGUID = '" + guid + "' AND ParentGUID <> '')";
                        isFirstLoad = false;
                    }
                    else
                    {
                        dtBac.Columns["GradeDetail_GradeID"].DefaultValue = gradeId;
                        dtBac.Columns["ParentGUID"].DefaultValue = guid;
                        dtBac.Columns["GradeDetail_BranchCode"].DefaultValue = Program.strBranchCode;
                        dtBac.DefaultView.RowFilter = "(GradeDetail_GradeID = " + gradeId.ToString() + " AND GradeDetail_GradeID > 0) OR (ParentGUID = '" + guid + "' AND ParentGUID <> '')";
                    }
                }
            }));
            //
            arrColumn = new List<DataGridViewColumn>();

            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("STT", "RN", 30, "", true));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("ParentGUID", "ParentGUID", 30, "", true));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("GradeDetail_ID", "GradeDetail_ID", 30,"",true));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("GradeDetail_GradeID", "GradeDetail_GradeID", 30,"",true));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("BranchCode", "GradeDetail_BranchCode", 30, "", true));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Level_ID", "GradeDetail_LevelId", 30,"",true));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Tên bậc", "GradeDetail_LevelName", 180));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("HS/Tiền", "GradeDetail_Amount", 80));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Số tháng", "GradeDetail_Month", 80));

            //
            m_GridHandle_Bac = FormGlobals.Grid_Init(dgvBac, arrColumn, "", 1);
            m_GridHandle_Bac.Row_Validated += new DataGridHandle.Row_ValidatedEventHandler(m_GridHandle_Bac_Row_Validated);
        }

        void m_GridHandle_Ngach_Row_Deleting(DataGridViewRow row, ref bool cancel)
        {
            //Xóa các dòng bên grid bậc tương ứng với ngạch xóa
        }

        void m_GridHandle_Ngach_Row_Validated(DataGridViewRow row)
        {
            if (String.IsNullOrEmpty(Globals.Object_GetString(row.Cells["GradeGUID"].Value, "")))
                row.Cells["GradeGUID"].Value = Guid.NewGuid().ToString();
            //

            //Kiểm tra các giá trị nhập
            row.Cells["RN"].Value = row.Index + 1;
        }

        void m_GridHandle_Bac_Row_Validated(DataGridViewRow row)
        {
            row.Cells["RN"].Value = row.Index + 1;
        }

        //Load danh sách
        private void LoadNgach()
        {
            try
            {
                int itemCount, currPage, pageSize;
                pageSize = 99999;
                //
                DataSet ds = Grade_Controller.Instance().Grade_GetList(out itemCount, Program.strBranchCode, 0, pageSize);
                dtNgach = ds.Tables[0];
                //MessageBox.Show("So luong dong:" + dtData.Rows.Count + " so luong table " + ds.Tables.Count);
                dtNgach.Columns["Grade_BranchCode"].DefaultValue = Program.strBranchCode;
                Globals.DataTable_RowNumber(dtNgach);
                dtNgach.Columns.Add("GradeGUID");
                //dtData.DefaultView.Sort = "Grade_Name";
                //Globals.DataTable_SetAutoColumn(dtData, "STT");
                try
                {
                    Globals.DataTable_SetUnique(dtNgach, "Grade_BranchCode", "Grade_Name");
                }
                catch (Exception ex)
                {
                    FormGlobals.Message_Warning("Tồn tại ngạch trùng tên");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            //
            dgvNgach.DataSource = dtNgach;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (FormGlobals.Control_CheckError(dgvNgach))
                FormGlobals.App_ProgressShow(delegate()
                {
                    Grade_Controller.Instance().Clear(Program.strBranchCode);
                    Grade_Controller.Instance().InsertTable(dtNgach);


                    //Dựa trên guid của thằng cha, để tìm ra Id mới của cha để insert update vào detail
                    Grade_Controller.Instance().SaveDetail(dtBac, dtNgach);
                    //Lưu bậc

                    //Load lại danh sách
                    LoadNgach();
                }, "Lưu dữ liệu", "Lưu thành công");
        }
    }
}
